
import java.util.Scanner;


public class AG5 {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int count = scan.nextInt();
		int[] o = new int[count];
		int[] dp = new int[count];
		while (--count >= 0) o[o.length - count - 1] = scan.nextInt();
		for (int i = 0; i < dp.length; i++) dp[i] = o[i];
		//1 7 3 5 9 4 8
		for (int i = 0; i < dp.length; i++) {
			for (int j = 0; j <= i; j++) {
				if (o[i] > o[j]) dp[i] = Math.max(dp[j] + o[i], dp[i]);
		
			}
		}
		int max = 0;
		for (int i = 0; i < dp.length; i++) max = Math.max(dp[i], max);
		System.out.println(max);
		scan.close();
	}
	
}
